<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/24 0024
 * Time: 上午 10:42
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\Goods;

class Stats extends Controller {
    /*
     * 商品分类统计
     * */
    public function goodsclassify(){
        if($this->request->isAjax()){
            $id=$this->request->param('id'); //获取搜索框的值
            $where['father_id']=$id;
            $data=$this->get_erji($where);
            return json($data);
        }else{
            /*一级分类*/
            $classify=Db::table("ec_goodsclassify")->where('father_id',0)->order('id asc')->select();
            $where['father_id']=$classify[0]['id'];
            $name=$classify[0]['classify_name'];
            $data=$this->get_erji($where);
            $this->assign('num',json_encode($data['num'],true));
            $this->assign('goodsclass',json_encode($data['goodsclass'],true));
            $this->assign('classify',$classify);
            $this->assign('name',$name);
            return $this->fetch();
        }

    }

    public function get_erji($where){
        //查询二级分类
        $erji=Db::name('goodsclassify')->where($where)->select();
        foreach ($erji as $k=>$v){
            $wheres['classify_id'] = $v['id'];
            $goodsclass[$k]=$v['classify_name'];
            $goodsmode=new Goods();
            $num[$k]=$goodsmode->where($wheres)->count();
        }
        $data['num']=$num;
        $data['goodsclass']=$goodsclass;
        return $data;
    }
}